package com.ht.medicaltreatment.viewmodel

import com.blankj.utilcode.util.LogUtils
import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.ht.medicaltreatment.bean.CreateSampleBoxBean
import com.ht.medicaltreatment.bean.Row2
import com.ht.medicaltreatment.bean.SampleBoxDetailBean
import com.ht.medicaltreatment.bean.SampleBoxDetailData
import com.ht.medicaltreatment.bean.SampleBoxListBean
import com.ht.medicaltreatment.bean.SuccessBean
import com.ht.medicaltreatment.http.api.DeleteSampleBoxAPI
import com.ht.medicaltreatment.http.api.SampleBoxDetailAPI
import com.ht.medicaltreatment.http.api.SampleBoxListAPI
import com.ht.medicaltreatment.http.api.SaveSampleBoxAPI
import com.ht.medicaltreatment.http.api.SaveSampleBoxUpdateAPI

class SampleBoxViewModel:BaseViewModel() {

    //创建样本盒
    fun createSampleBox(api: SaveSampleBoxAPI,onResult:()->Unit){
        EasyHttp.post(this)
            .api(api)
            .request(object : OnHttpListener<CreateSampleBoxBean> {
                override fun onHttpSuccess(result: CreateSampleBoxBean) {
                    if(result.code == 200){
                        onResult()
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message!!)
                }
            })
    }

    fun sampleBoxList(currentPage: Int,name:String,uid:String,onResult:(MutableList<Row2>)->Unit){
        val api = SampleBoxListAPI(currentPage,"20",name,uid)
        EasyHttp.post(this)
            .api(api)
            .request(object : OnHttpListener<SampleBoxListBean> {
                override fun onHttpSuccess(result: SampleBoxListBean) {
                    if(result.code == 200){
                        onResult(result.rows)
                    }else{
                        dismissProgress(result.msg)
                    }
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message!!)
                }
            })
    }

    fun delete(id: Int,onResult:()->Unit){
        val ids = mutableListOf(id)
        EasyHttp.post(this)
            .api(DeleteSampleBoxAPI(ids))
            .request(object : OnHttpListener<SuccessBean> {
                override fun onHttpSuccess(result: SuccessBean?) {
                    LogUtils.i(result?.message)
                    if(result?.code == 200){
                        onResult()
                    }else{
                        dismissProgress(result?.message?:"接口请求错误")
                    }
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message!!)
                }
            })
    }

    fun detail(id:Int,onResult:(SampleBoxDetailData)->Unit){
        EasyHttp.post(this)
            .api(SampleBoxDetailAPI(id))
            .request(object :OnHttpListener<SampleBoxDetailBean>{
                override fun onHttpSuccess(result: SampleBoxDetailBean) {
                    if(result.code == 200){
                        onResult(result!!.data)
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: java.lang.Exception) {
                    dismissProgress(e.message!!)
                }
            })
    }

    //修改
    fun update(api: SaveSampleBoxUpdateAPI,onResult: () -> Unit){
        EasyHttp.post(this)
            .api(api)
            .request(object : OnHttpListener<SuccessBean> {
                override fun onHttpSuccess(result: SuccessBean) {
                    if(result.code == 200){
                        onResult()
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message!!)
                }
            })
    }
}
